﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using AutoMapper;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Sautom.ClientComunication;
using Sautom.ClientComunication.CommandService;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Client.Controls.ViewModels;
using Sautom.Modules.Client.Models;

namespace Sautom.Modules.Client.ViewModels
{
    public sealed class AirlineTicketViewModel : NavigationAwareNotificationObject
    {
        #region Constructor
        public AirlineTicketViewModel(IRegionManager regionManager, ServiceFactory serviceFactory)
        {
            RegionManager = regionManager;

            QueriesServiceClient = serviceFactory.GetQueriesService();
            CommandServiceClient = serviceFactory.GetCommandsService();

            BackCommand = new DelegateCommand(ExecuteBackCommand);
        }

        #endregion

        #region Commands
        public DelegateCommand BackCommand { get; private set; }

        private void ExecuteBackCommand()
        {
            var uri = new Uri(PathProvider.ClientDetails +
                new UriQuery
                    {
                        {
                            "clientId",
                            ClientId.ToString()
                            }
                    },
                UriKind.Relative);
            RegionManager.RequestNavigate(RegionProvider.MainRegion, uri);
        }
        #endregion

        #region Properties
        private IRegionManager RegionManager { get; set; }
        private IQueriesService QueriesServiceClient { get; set; }
        private ICommandService CommandServiceClient { get; set; }
        #endregion

        #region ViewModel Properties
        public Guid ClientId { get; set; }
        
        public Guid Id { get; set; }

        private string _departureCity;
        public string DepartureCity
        {
            get { return _departureCity; }
            set
            {
                _departureCity = value;
                RaisePropertyChanged(() => DepartureCity);
            }
        }

        private DateTime? _departureDate;
        public DateTime? DepartureDate
        {
            get { return _departureDate; }
            set
            {
                _departureDate = value;
                RaisePropertyChanged(() => DepartureDate);
            }
        }

        private string _arrivalCity;
        public string ArrivalCity
        {
            get { return _arrivalCity; }
            set
            {
                _arrivalCity = value;
                RaisePropertyChanged(() => ArrivalCity);
            }
        }

        private DateTime? _arrivalDate;
        public DateTime? ArrivalDate
        {
            get { return _arrivalDate; }
            set
            {
                _arrivalDate = value;
                RaisePropertyChanged(() => ArrivalDate);
            }
        }

        private DateTime? _bookingDate;
        public DateTime? BookingDate
        {
            get { return _bookingDate; }
            set
            {
                _bookingDate = value;
                RaisePropertyChanged(() => BookingDate);
            }
        }

        private DateTime? _bookingExpireDate;
        public DateTime? BookingExpireDate
        {
            get { return _bookingExpireDate; }
            set
            {
                _bookingExpireDate = value;
                RaisePropertyChanged(() => BookingExpireDate);
            }
        }

        private DateTime? _redemptionDate;
        public DateTime? RedemptionDate
        {
            get { return _redemptionDate; }
            set
            {
                _redemptionDate = value;
                RaisePropertyChanged(() => RedemptionDate);
            }
        }

        public Guid OrderId { get; set; }
        #endregion

        #region INavigationAware

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            base.OnNavigatedTo(navigationContext);

            string orderId = navigationContext.Parameters["orderId"];
            string clientId = navigationContext.Parameters["clientId"];
            if (String.IsNullOrWhiteSpace(orderId) || String.IsNullOrWhiteSpace(clientId)) return;

            OrderId = Guid.Parse(orderId);
            ClientId = Guid.Parse(clientId);

            var existingTicket = QueriesServiceClient.AirlineTicketForView(OrderId);
            AirlineTicketEditDtoOutput ticketForEdit = QueriesServiceClient.AirlineTicketForEdit(existingTicket == null ? Guid.Empty : existingTicket.Id);

            Mapper.CreateMap<AirlineTicketEditDtoOutput, AirlineTicketViewModel>();
            Mapper.Map(ticketForEdit, this);
        }

        public override void OnNavigatedFrom(NavigationContext navigationContext)
        {
            base.OnNavigatedFrom(navigationContext);
            
            Mapper.CreateMap<AirlineTicketViewModel, AirlineTicketEditDtoInput>();
            var ticket =  Mapper.Map<AirlineTicketEditDtoInput>(this);

            CommandServiceClient.EditOrAddAirlineTicket(ticket);
        }

        #endregion
    }
}
